package cn.tiancaifan.micro.controller.system.basic;


import cn.tiancaifan.micro.entity.Job;
import cn.tiancaifan.micro.entity.SystemResults;
import cn.tiancaifan.micro.service.JobService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/basic/job")
public class JobController {

    @Autowired
    JobService jobService;

    @GetMapping("/")
    public SystemResults getAllJobs(){
        List<Job> allJobs = jobService.getAllJobs();
        return  SystemResults.ok(null,allJobs);
    }

    @PutMapping("/")
    public SystemResults updataJob(@RequestBody Job job){
        int i = jobService.updataJob(job);
        if (i == 1){
            return SystemResults.ok("修改成功");
        }else {
            return SystemResults.error("修改失败");
        }
    }

    @PostMapping("/")
    public SystemResults PostMapping(@RequestBody Job job){
        if (jobService.addJob(job) == 1){
            return SystemResults.ok("添加成功");
        }else {
            return SystemResults.error("添加失败");
        }
    }

    @DeleteMapping("/{id}")
    public SystemResults deleteJobById(@PathVariable Integer id){
        if (jobService.deleteJobById(id) == 1){
            return SystemResults.ok("删除成功");
        }else {
            return SystemResults.ok("删除失败");
        }
    }

    @DeleteMapping("/")
    public SystemResults deleteJobsByIds(@Param("ids") Integer[] ids){
        if (jobService.deleteJobsByIds(ids) == ids.length){
            return SystemResults.ok("删除成功");
        }else {
            return SystemResults.ok("删除失败");
        }
    }
}
